package org.hawk.gwt.ppc.reflect.lang;

import org.hawk.gwt.ppc.reflect.emul.java.lang.reflect.Type;
import org.hawk.gwt.ppc.reflect.emul.java.lang.reflect.WildcardType;


/**
 * WildcardType implementation for internal purposes.
 * Clients should never instantiate this class.
 * 
 * @author alex.bereznevatiy@gmail.com
 */

public class GWTWildCardType implements WildcardType {
	
	private Object lower;
	private Object[] upper;
	
	public GWTWildCardType(Object accessor, Object lower, Object...upper) {
		GWTParameterizedType.checkAccess(accessor);
		this.lower = lower;
		this.upper = upper;
	}

	public Type[] getUpperBounds() {
		Type[] result = new Type[upper.length];
		for (int i = 0; i < upper.length; i++) {
			if (upper[i] instanceof Class) {
				result[i] = ClassDelegate.forClass((Class<?>)upper[i]);
			} else {
				result[i] = (Type)upper[i];
			}
		}
		return result;
	}

	public Type[] getLowerBounds() {
		if (lower == null) {
			return new Type[0];
		}
		if (lower instanceof Class) {
			return new Type[]{ClassDelegate.forClass((Class<?>)lower)};
		}
		return new Type[]{ (Type)lower };
	}

	@Override
	public String toString() {
		if (lower != null) {
			return "? super " + GWTParameterizedType.toString(lower);
		}
		if (upper.length > 0 && !upper[0].equals(Object.class)) {
			return "? extends " + GWTParameterizedType.toString(upper[0]);
		}
		return "?";
	}
}
